package one.bugu.android.demon.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.app.Fragment;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

public class IntentUtils {
    private IntentUtils() {
    }

    public static <T> void startAty(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    public static void startAty(Context context, Class<?> cls, String key, String value) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void startAty(Context context, Class<?> cls, String key, Serializable obj) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key, obj);
        context.startActivity(intent);
    }

    public static void startAty(Context context, Class<?> cls, String key, Parcelable obj) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key, obj);
        context.startActivity(intent);
    }

    public static void startAty(Context context, Class<?> cls, List<ParamUtils.NameValue> extras) {
        Intent intent = new Intent(context, cls);
        for (ParamUtils.NameValue item : extras) {
            setValueToIntent(intent, item.name, item.value);
        }
        context.startActivity(intent);
    }

    public static void startService(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startService(intent);
    }

    public static void stopService(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.stopService(intent);
    }

    public static void startAtyForResult(Activity aty, Class<?> cls, int requestCode) {
        Intent intent = new Intent(aty, cls);
        aty.startActivityForResult(intent, requestCode);
    }

    public static void startAtyForResult(Fragment fragment, Class<? extends Activity> cls, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), cls);
        fragment.startActivityForResult(intent, requestCode);
    }

    public static void startAtyForResult(Activity aty, Class<?> cls, String key, String value, int requestCode) {
        Intent intent = new Intent(aty, cls);
        intent.putExtra(key, value);
        aty.startActivityForResult(intent, requestCode);
    }

    public static void startAtyForResult(Activity aty, Class<?> cls, List<ParamUtils.NameValue> extras, int requestCode) {
        Intent intent = new Intent(aty, cls);
        for (ParamUtils.NameValue item : extras) {
            setValueToIntent(intent, item.name, item.value);
        }
        aty.startActivityForResult(intent, requestCode);
    }

    public static void startAtyForResult(Fragment fragment, Class<?> cls, List<ParamUtils.NameValue> extras, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), cls);
        for (ParamUtils.NameValue item : extras) {
            setValueToIntent(intent, item.name, item.value);
        }
        fragment.startActivityForResult(intent, requestCode);
    }

//    public static void startAtyClearTop(Activity context, Class<?> cls)
//    {
//        Intent intent = new Intent(context, cls);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        context.startActivity(intent);
//    }

    public static void startDialNumberIntent(Activity context, String phoneNumber) {
        context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)));
    }

    public static void startSMSIntent(Activity context, String phoneNumber, String body) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + phoneNumber)));
    }

    public static void startSettingIntent(Context context) {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        context.startActivity(intent);
    }

    public static void startBrowserIntent(Context context, String url) {
        if (url == null) {
            return;
        }
        if (!url.contains("http://") && !url.contains("https://")) {
            url = "http://" + url;
        }
        Intent intent = new Intent();
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        intent.setAction(Intent.ACTION_VIEW);
        context.startActivity(intent);
    }

    public static void choicePic(Activity aty, int requestCode) {
        Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        aty.startActivityForResult(pickIntent, requestCode);
    }

    public static void startCamera(Activity aty, int requestCode, String storeFilePath) {
        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(aty.getExternalCacheDir(), storeFilePath)));
        aty.startActivityForResult(takeIntent, requestCode);
    }

    public static void startCropPhoto(Activity aty, Uri uri, int requestCode) throws IOException {
        Bitmap photo = MediaStore.Images.Media.getBitmap(aty.getContentResolver(), uri);
        int width = photo.getWidth();
        int height = photo.getHeight();
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        //        intent.putExtra("scale", true);// 去黑边
        intent.putExtra("scaleUpIfNeeded", true);// 去黑边
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 0.5625);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高，切忌不要再改动下列数字，会卡死
        //横屏
        if (width > height) {
            intent.putExtra("outputX", 600);
            intent.putExtra("outputY", 337);
        } else {
            intent.putExtra("outputX", 600);
            intent.putExtra("outputY", 600);
        }
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//这个uri没有改变的话，是在原图上编辑
        intent.putExtra("return-data", false);//设置为不返回数据
        aty.startActivityForResult(intent, requestCode);
    }

    private static void setValueToIntent(Intent intent, String key, Object val) {
        if (null == key || null == val) {
            return;
        }
        if (val instanceof Boolean) {
            intent.putExtra(key, (Boolean) val);
        } else if (val instanceof Boolean[]) {
            intent.putExtra(key, (Boolean[]) val);
        } else if (val instanceof String) {
            intent.putExtra(key, (String) val);
        } else if (val instanceof String[]) {
            intent.putExtra(key, (String[]) val);
        } else if (val instanceof Integer) {
            intent.putExtra(key, (Integer) val);
        } else if (val instanceof Integer[]) {
            intent.putExtra(key, (Integer[]) val);
        } else if (val instanceof Long) {
            intent.putExtra(key, (Long) val);
        } else if (val instanceof Long[]) {
            intent.putExtra(key, (Long[]) val);
        } else if (val instanceof Double) {
            intent.putExtra(key, (Double) val);
        } else if (val instanceof Double[]) {
            intent.putExtra(key, (Double[]) val);
        } else if (val instanceof Float) {
            intent.putExtra(key, (Float) val);
        } else if (val instanceof Float[]) {
            intent.putExtra(key, (Float[]) val);
        } else {
            throw new IllegalArgumentException("Not support data Type!");
        }
    }
}
